<?php

declare (strict_types=1);
namespace App\Model;

use App\Constants\Constant;
use Carbon\Carbon;
use Hyperf\DbConnection\Model\Model;
/**
 * @property int $user_id 
 * @property int $total 
 * @property int $available 
 * @property int $consume 
 */
class UserScore extends Model
{
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'user_score';
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [];
    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = ['user_id' => 'integer', 'total' => 'integer', 'available' => 'integer', 'consume' => 'integer'];

    protected $primaryKey = "user_id";

    public $timestamps = false;

    protected $appends = ['today_inc_score','tasks'];

    public function getTodayIncScoreAttribute(){
        return $this->details()->where('direction',Constant::DIRECTION_INC)->whereDate("add_time",Carbon::now()->toDateString())->sum("score");
    }

    public function getTasksAttribute(){
        $data = $this->details()->where("direction",Constant::DIRECTION_INC)->whereDate("add_time",Carbon::now()->toDateString())->get();

        return [
            'sign'=>$data->where("origin",Constant::SCORE_ORIGIN_SIGN)->count(),
            'activity_join'=>$data->where("origin",Constant::SCORE_ORIGIN_ACTIVITY_JOIN)->sum("score"),
            'activity_share'=>$data->where("origin",Constant::SCORE_ORIGIN_ACTIVITY_SHARE)->count(),
            'mini_share'=>$data->where("origin",Constant::SCORE_ORIGIN_MINI_SHARE)->count(),
            'foot_publish'=>$data->where("origin",Constant::SCORE_ORIGIN_FOOT_PUBLISH)->count(),
            'foot_excellent'=>$data->where("origin",Constant::SCORE_ORIGIN_FOOT_EXCELLENT)->sum("score")
        ];
    }

    public function details(){
        return $this->hasMany(UserScoreDetail::class,"user_id","user_id");
    }
}